package com.web;

import com.common.Message;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 页面跳转控制器
 *
 * Created by Administrator on 2018-8-20 0020.
 */
@Controller
@RequestMapping("/home")
public class BaseController {

    private static final Logger log = LogManager.getLogger(BaseController.class);

    @RequestMapping("/vueData")
    private String vueData(ModelMap modelMap) {
        modelMap.addAttribute("title", "vue表格操作");
        return "vue-data";
    }

    @RequestMapping("/toIndex")
    private String toIndex(ModelMap modelMap) {
        modelMap.addAttribute("title", "主页");
        return "index";
    }

    @RequestMapping("/login")
    private String login(ModelMap modelMap) {
        modelMap.addAttribute("title", "用户登录");
        return "login";
    }

    /**
     * 用户页面
     *
     * @return
     */
    @RequestMapping("/user")
    private String user() {
        log.info("user页面");
        return "user";
    }

    /**
     * 用户注册
     *
     * @return
     */
    @RequestMapping("/register")
    private String register() {
        log.info("进入注册页面");
        return "register";
    }

    /**
     * 跳转到用户注册
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/toAddUser")
    private String toAddUser(ModelMap modelMap) {
        modelMap.addAttribute("title", "用户注册");
        return "useradd";
    }

    @RequestMapping("/testUser")
    private String testUser(ModelMap modelMap) {
        log.info("测试velocity页面");
        modelMap.addAttribute("test", "测试配置velocity是否成功！");
        modelMap.addAttribute("title", "测试");
        return "test";
    }


    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @RequestMapping("/loginExit")
    private String loginExit(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(Message.LOGIN_SUCCESS_SESSION.getCode());
        session.invalidate();
        return "redirect:/home/login";
    }


}
